package com.listeningframework.boot.codegen.context;

import com.listeningframework.boot.codegen.CodeGenConfiguration;
import com.listeningframework.boot.codegen.CodeGenORM;
import com.listeningframework.boot.codegen.lang.ReflectUtils;

import java.io.File;
import java.net.URLClassLoader;
import java.util.Set;

/**
 * 
 * @description CodeGen Context
 *
 * @author Listening
 * @version $Id: CodeGenContext.java, v 0.1 2016年12月4日 下午6:55:07 Listening Exp $
 */
public class SystemContext {
    /**
     * Instance
     */
    private static final SystemContext INSTANCE = new SystemContext();
    /**
     * Class Loader
     */
    private URLClassLoader             classLoader;
    /**
     * CodeGenConfiguration Class
     */
    private Class<?>                   codeGenConfigureClz;
    /**
     * SRC源文件目录
     */
    private String                     srcPath;
    /**
     * 代码根目录
     */
    private String                     classPath;
    /**
     * CodeGen Entity Set
     */
    private Set<Class<?>>              entitySet;

    private SystemContext() {
    }

    /**
     * Getter method for property <tt>instance</tt>.
     * 
     * @return property value of INSTANCE
     */
    public static SystemContext getInstance() {
        return INSTANCE;
    }

    /**
     * Get ClassLoader
     */
    public static URLClassLoader getClassLoader() {
        return getInstance().classLoader;
    }

    /**
     * 设置ClassLoader
     */
    public static void setClassLoader(URLClassLoader classLoader) {
        getInstance().classLoader = classLoader;
    }

    /**
     * 获取代码生成全局参数注解
     */
    public static CodeGenConfiguration getCodeGenConfig() {
        return getInstance().codeGenConfigureClz.getAnnotation(CodeGenConfiguration.class);
    }

    /**
     * 持久化技术是否为MyBatis
     * 
     * @return boolean
     */
    public static boolean isORMMyBatis() {
        return CodeGenORM.isMyBatis(getCodeGenConfig().orm());
    }

    /**
     * 设置代碼生成全局參數
     */
    public static void setCodeGenConfigClz(Class<?> clz) {
        getInstance().codeGenConfigureClz = clz;
    }

    /**
     * 获取代码路径
     */
    public static String getClassPath() {
        return getInstance().classPath;
    }

    /**
     * 设置代码路径
     */
    public static void setClassPath(String classPath) {
        getInstance().classPath = classPath;
    }

    /**
     * 获取源代码目录
     */
    public static String getSrcPath() {
        return getInstance().srcPath;
    }

    /**
     * Maven项目Java源码目录
     * 
     * @return String
     */
    public static String getSrcJavaPath() {
        return getInstance().srcPath + "main" + File.separator + "java" + File.separator;
    }

    /**
     * Maven项目Resources源码目录
     * 
     * @return String
     */
    public static String getSrcResourcesPath() {
        return getInstance().srcPath + "main" + File.separator + "resources" + File.separator;
    }

    /**
     * 设置源代码目录
     */
    public static void setSrcPath(String srcPath) {
        getInstance().srcPath = srcPath;
    }

    /**
     * 获取Entity列表
     */
    public static Set<Class<?>> getEntitySet() {
        return getInstance().entitySet;
    }

    /**
     * 设置Entity列表
     */
    public static void setEntitySet(Set<Class<?>> entitySet) {
        getInstance().entitySet = entitySet;
    }

    /**
     * 获取代码根包
     */
    public static String getBasePackage() {
        Class<?> configClz = getInstance().codeGenConfigureClz;
        return ReflectUtils.getClassPackage(configClz);
    }

}
